import { Injectable } from '@angular/core';

import { TreeModule, TreeNode } from 'primeng/primeng';

@Injectable()
export class SelectionService {

  constructor() { }

  findInTree(trees, id) {
    let obj;
    for (let tree of trees) {
      if (tree.resCategoryId === id) {
        obj = tree;
        tree.expanded = true
        break;
      }
    }
    if (obj) return obj;
    for (let tree of trees) {
      if (tree.children) {
        obj = this.findInTree(tree.children, id);
        if (obj) break;
      }
    }
    return obj;
  }

  // 选中后台返回数据
  cheeck(nodes: TreeNode[], select: any[]) {
    let arr = [];
    for (let item of nodes) {
      item.expanded = false;
    }
    for (let item of select) {
      let a = this.findInTree(nodes, item.resId);
      if (a) {
        arr.push(a);
      }
    }
    for (let item of arr) {
      if (item.parent) {
        let has = false;
        for (let item2 of arr) {
          if (item2.resCategoryId === item.parent.resCategoryId) {
            has = true;
            break;
          }
        }
        if (!has) {
          item.parent.partialSelected = false;
          arr.push(item.parent);
        }
      }
    }
    //console.log(arr)
    return arr;
  }

  // // 只展开子节点选中的父节点
  // expandAllOnlyWithSelected(ids: Array<string>) {
  //   console.log(ids)
  //   let expandAll = (data) => {
  //     console.log(ids,data)
  //     if (data instanceof Array) {
  //       for (let item of data) {
  //         if (item.children) {
  //           item.children.forEach(_item => {
  //             if(ids.includes(_item.data)){
  //               item.expanded = true
  //             }
  //           })
  //           expandAll(item.children)
  //         }
  //       }
  //     }
  //   }
  //   expandAll(this.treeData)
  //   //console.log(this.data)
  // }

}
